﻿Ext.ns('Microblogging.controller');
//路由器配置
Ext.Router.draw(function(map){
	var config = App.controllers,
	view = config.VIEW,
	um = config.USER_MANAGER,
	mm = config.MESSAGE_MANAGER,
	oh = config.OAUTH;
	//视图控制
	map.connect('view/:ids' , {controller : view, action : 'showView'});
	map.connect('view/:ids/:anim' , {controller : view, action : 'showView'});
	map.connect('view/:ids/:anim/:app' , {controller : view, action : 'showView'});
	//用户管理
	map.connect('user' , {controller : um , action : 'userList'});
	map.connect('user/check' , {controller : um , action : 'checkLogin'});
	map.connect('user/add/:type' , {controller : um , action : 'addUser'});
	map.connect('user/del/:userIds' , {controller : um , action : 'delUser'});
	map.connect('user/status/:userIds/:status' , {controller : um , action : 'setStatus'});
	map.connect('user/edit/:userId' , {controller : um , action : 'editUser'});
	map.connect('user/set/:userId' , {controller : um , action : 'setting'});
	//OAuth身份验证
	map.connect('user/oauth/sign/:type',{controller: oh,action:'sign'});
	map.connect('user/oauth/login/:type',{controller: oh,action:'login'});
	//主页
	map.connect('home' , {controller : mm , action : 'home'});
	map.connect('message' , {controller : mm , action : 'show'});
	
	//随便看看
	map.connect('public' , {controller : mm , action : 'publicTimeline'});
});
//拦截用户合法性
Ext.Dispatcher.addListener('before-dispatch',function(interaction){
	if(interaction.action == 'addUser' || interaction.action == 'publicTimeline'){
		return;
	}
	//TODO 检查用户数量
	var ucount = 1;
	if(ucount < 1){
		Ext.redirect('user/add');
		return false;
	}
});
//重写redirect方法
Ext.override(Ext.util.Dispatcher,{
	 redirect: function(options,useHistory) {
        if (options instanceof Ext.data.Model) {
            //compose a route for the model
        } else if (typeof options == 'string') {
            //use router
            var route = Ext.Router.recognize(options);
            if (route) {
	            //是否使用历史记录
	            if(useHistory === false){
	            	route.historyUrl = false;
	            }
                return this.dispatch(route);
            }
        }
        return null;
    }
});
//调用视图模块公用方法
//Microblogging.controller.showView = function(o , id , anim , app){
//	var app = app || this.application,
//	umView = app.getView(id || this.id);
//	if(o && !(o instanceof Ext.Interaction)){
//		o = umView.getComponent(o);
//	}else{
//		o = null;
//	}
//	var mian = app.viewport.setActiveItem(umView , o ? null : anim);
//	return o ? umView.setActiveItem(o , o ? anim : null ) : mian;
//}